package com.hdjk.view.callback;

import com.hdjk.view.entity.BuyGroupResultEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

import static android.R.id.list;

/**
 * Created by Admin on 2017/10/25.
 */

public abstract class BuyGroupCallback extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int id)
            throws Exception {
        Msg msg = super.parseNetworkResponse(response, id);
        JSONObject object = (JSONObject) msg.getData();
        if (object != null) {
            BuyGroupResultEntity entity = new BuyGroupResultEntity();
            if (object != null) {
                if (!object.isNull("merchantNumber")) {
                    entity.setMerchantNumber(object.getString("merchantNumber"));
                }
                if (!object.isNull("portfolioCode")) {
                    entity.setPortfolioCode(object.getString("portfolioCode"));
                }
                if (!object.isNull("portfolioId")) {
                    entity.setPortfolioId(object.getString("portfolioId"));
                }
                if (!object.isNull("details")) {
                    JSONArray array = object.getJSONArray("details");
                    List<BuyGroupResultEntity.Details> list = new ArrayList<>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject obj = array.getJSONObject(i);
                        BuyGroupResultEntity.Details details = entity.new Details();
                        if (!obj.isNull("contractNumber")) {
                            details.setContractNumber(obj.getString("contractNumber"));
                        }
                        if (!obj.isNull("fundCode")) {
                            details.setFundCode(obj.getString("fundCode"));
                        }
                        if (!obj.isNull("fundName")) {
                            details.setFundName(obj.getString("fundName"));
                        }
                        if (!obj.isNull("investmentAmount")) {
                            details.setInvestmentAmount(obj.getString("investmentAmount"));
                        }
                        if (!obj.isNull("orderDate")) {
                            details.setOrderDate(obj.getString("orderDate"));
                        }
                        if (!obj.isNull("reason")) {
                            details.setReason(obj.getString("reason"));
                        }
                        if (!obj.isNull("transactionCfmLagDay")) {
                            details.setTransactionCfmLagDay(obj.getString("transactionCfmLagDay"));
                        }
                        if (!obj.isNull("transactionDate")) {
                            details.setTransactionDate(obj.getString("transactionDate"));
                        }
                        if (!obj.isNull("transactionStatus")) {
                            details.setTransactionStatus(obj.getString("transactionStatus"));
                        }
                        if (!obj.isNull("transactionType")) {
                            details.setTransactionType(obj.getString("transactionType"));
                        }
                        list.add(details);
                    }
                    entity.setDetails(list);
                }
            }
            msg.setData(list);
        }
        return msg;
    }
}